var express = require('express');
var router = express.Router();
var request = require('request');
var path = require('path');

router.get('/balance',function(req,res) { //查询余额
    if(req.session.token) {
        res.sendFile(path.join(__dirname,"../src/templates/balance.html"));
    }else {
        res.redirect("/");
    }
});
router.get('/accountBalance',function(req,res) { //查额ajax
    var url = "http://39.106.9.239/ATM/business/query/accountBalance";
    if(req.session.token) {
        request({
            url: url,
            method : "get",
            headers: {
                "authorization" : req.session.token,
                "deviceNo" : 100
            }
        },function(_err,_res,_resBody) {
            if(!_err && _res.statusCode == 200) {
               res.send(_resBody);
            }else {
                console.log(_err);
            }
        });
    }else {
        res.redirect("/");
    }
});

router.get('/advice',function(req,res) {//打印凭条
    if(req.session.token) {
        var url = "http://39.106.9.239/ATM/business/query/advice/"+req.session.adviceId;
        console.log(req.session.adviceId);
        request({
            url : url,
            method: "get",
            headers: {
                "authorization" : req.session.token,
                "deviceNo" : 100, 
            }       
        },function(_err,_res,_resBody) {
            res.send(_resBody);
        });
    }else {
        res.redirect("/");
    }
});

router.get('/detailed',function(req,res) { //明细查询
    if(req.session.token) {
        res.sendFile(path.join(__dirname,"../src/templates/detailed.html"));
    }else {
        res.redirect("/");
    }
});
router.get('/statement',function(req,res) { //明细查询ajax
    var url = "http://39.106.9.239/ATM/business/query/statement"
    if(req.session.token) {
        request({
            url: url,
            method : "get",
            headers: {
                "authorization" : req.session.token,
                "deviceNo" : 100
            }
        },function(_err,_res,_resBody) {
            if(!_err && _res.statusCode == 200) {
               res.send(_resBody);
            }else {
                console.log(_err);
            }
        });
    }else {
        res.redirect("/");
    }
});

module.exports = router;